package com.imooc.dao;

import com.imooc.bean.Message;
import com.imooc.config.PageConfig;
import com.imooc.db.DBAccess;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 和message表相关的数据库操作
 */
public class MessageDao {

    public List<Message> queryList(String command, String description) {
        DBAccess dbAccess = new DBAccess();
        SqlSession session = null;
        List<Message> messageList = null;
        try {
            session = dbAccess.getSqlSession();
            Message message = new Message();
            message.setCommand(command);
            message.setDescription(description);
            IMessage mapper = session.getMapper(IMessage.class);
//            messageList = session.selectList("Message.queryMessageList", message);
            messageList = mapper.queryMessageList(message);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                session.close();
        }
        return messageList;
    }

    public List<Message> queryListByPage(Map<String, Object> parameter) {
        DBAccess dbAccess = new DBAccess();
        SqlSession session = null;
        List<Message> messageList = null;
        try {
            session = dbAccess.getSqlSession();
            IMessage mapper = session.getMapper(IMessage.class);
            messageList = mapper.queryMessageListByPage(parameter);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                session.close();
        }
        return messageList;
    }
    /**
     * 根据主键删除一条信息
     * @param id
     */
    public void deleteOne(int id) {
        DBAccess dbAccess = new DBAccess();
        SqlSession session = null;
        try {
            session = dbAccess.getSqlSession();
            session.delete("Message.deleteOne", id);
            session.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                session.close();
        }
    }

    public void deleteBatch(List<Integer> ids) {
        DBAccess dbAccess = new DBAccess();
        SqlSession session = null;
        try {
            session = dbAccess.getSqlSession();
            session.delete("Message.deleteBatch", ids);
            session.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                session.close();
        }
    }

//    public List<Message> queryList(String command, String description) {
//        List<Message> messageList = new ArrayList<>();
//        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            String url = "jdbc:mysql:///micro_message?useSSL=true";
//            Connection conn = DriverManager.getConnection(url, "root", "123456");
//            StringBuilder sql = new StringBuilder("select id, command, description, content from message where 1=1 ");
//            List<Object> params = new ArrayList<>();
//            if (command != null && !"".equals(command.trim())) {
//                sql.append("and command = ? ");
//                params.add(command);
//            }
//
//            if (description != null && !"".equals(description.trim())) {
//                sql.append("and description like '%' ? '%' ");
//                params.add(description);
//            }
//
//            PreparedStatement ps = conn.prepareStatement(sql.toString());
//            for (int i = 1; i <= params.size(); i++) {
//                ps.setString(i, params.get(i - 1).toString());
//            }
//            ResultSet rs = ps.executeQuery();
//            Message msg = null;
//            while (rs.next()) {
//                msg = new Message();
//                msg.setId(rs.getInt("id"));
//                msg.setCommand(rs.getString("command"));
//                msg.setDescription(rs.getString("description"));
//                msg.setContent(rs.getString("content"));
//                messageList.add(msg);
//            }
//            rs.close();
//            ps.close();
//            conn.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return messageList;
//    }
}
